<?php
namespace app\web\home;


use app\web\model\Scheduling as myModel;
use app\web\model\Technician;

class Scheduling extends Base 
{
    /**
     * 获取最经7天的时间
     * [getDate description]
     * @return [type] [description]
     */
    public function getDate()
    {
        return show(200,'最近七天',myModel::getDate());
    }
    public function get($date,$t_id = 0)
    {
        if($this->appkey == 1){
            $map[] = ['a.user_id','=',$this->uid];
        }else{
            $map[] = ['a.id','=',$t_id];
        }
        $info = Technician::alias('a')->where($map)
                            ->leftjoin('shop b','b.id=a.shop_id')
                            ->field('a.id,a.shop_id,b.business_start,b.business_end')
                            ->find();
             
        $info['business_start'] = $info['business_start'] ? $info['business_start'] : '00:00';
        $info['business_end'] = $info['business_end'] ? ($info['business_end'] == '00:00' ? '24:00' : $info['business_end']) : '24:00';
        $time = time();
        $list = myModel::where(['technician_id'=>$info['id'],'scheduling_date'=>strtotime($date)])
                        // ->where('start_time','>',time())
                        ->field('id,FROM_UNIXTIME(start_time,"%H:%i") as start_time,start_time as start_time1,if(start_time-900<'.$time.',1,status) as status')
                        ->order('start_time')
                        ->select()
                        ->toArray();
        if(!$list){
            $arr = $this->getBusiness($info['business_start'],$info['business_end'],$date,$info['id'],[]);
            $list = (new myModel())->saveAll($arr);
            $list = myModel::where(['technician_id'=>$info['id'],'scheduling_date'=>strtotime($date)])
                            // ->where('start_time','>',time())
                            ->field('id,FROM_UNIXTIME(start_time,"%H:%i") as start_time,if(start_time-900<'.$time.',1,status) as status')
                            ->order('start_time')
                            ->select()->toArray();
        }
        return show(200,'排班表',$list);
    }
    /**
     * 修改排班表
     * [edit description]
     * @return [type] [description]
     */
    public function edit(){
        if($this->auth_status == 2) return show(400,'无操作权限');
        $post = input('post.');
        $res = (new myModel())->allowField('status')->saveAll($post['data']);
        if($res){
            return show(200,'修改成功');
        }
        return show(400,'修改失败');  
    }
    /**
     * 清空所有排班
     * [emptyScheduling description]
     * @param  integer $t_id [description]
     * @return [type]        [description]
     */
    public function emptyScheduling($t_id = 0)
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        $t_id = $this->appkey == 1 ? $this->getCurrentId() : $t_id;
        $res = myModel::where([['technician_id','=',$t_id],['status','=',1],['start_time','>',time()]])->update(['status'=>0]);
        return show(200,'已清空');
    }
    /**
     * 全闲全忙 
     * [allScheduling description]
     * @param  [type]  $status [description]
     * @param  [type]  $date   [description]
     * @param  integer $t_id   [description]
     * @return [type]          [description]
     */
    public function allScheduling($status,$date,$t_id = 0)
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        $t_id = $this->appkey == 1 ? $this->getCurrentId() : $t_id;
        $where[] = ['technician_id','=',$t_id];
        $where[] = ['scheduling_date','=',strtotime($date)];
        $where[] = ['status','=',$status == 1 ? 0 : 1];
        $res = myModel::where($where)->update(['status'=>$status]);
        return show(200,'ok');
    }
    /**
     * 设置七天相同配置
     * [identical description]
     * @param  [type]  $date [description]
     * @param  integer $t_id [description]
     * @return [type]        [description]
     */
    public function identical($date,$t_id = 0)
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        $date = strtotime($date);
        $t_id = $this->appkey == 1 ? $this->getCurrentId() : $t_id;
        $myscheduling = myModel::where(['technician_id'=>$t_id,'scheduling_date'=>$date])->field('id,FROM_UNIXTIME(start_time,"%H:%i") as start_time,status')->order('start_time')->select()->toArray();
        $myscheduling = array_column($myscheduling,null,'start_time');
        $date7 = myModel::getDate();
        $date7 = array_column($date7, 'ymd');
        array_walk($date7, function(&$v){
            $v = strtotime($v);
        });
        $list = myModel::where(['technician_id'=>$t_id])->where('scheduling_date','in',$date7)->field('id,FROM_UNIXTIME(start_time,"%H:%i") as start_time,status,scheduling_date')->order('start_time')->select()->toArray();
        $date8 = array_diff($date7,array_unique(array_column($list, 'scheduling_date')));
        $update_data = [];
        foreach ($list as $key => $value) {
            if($value['scheduling_date'] == $date) continue;
            if($value['status'] != 2 && isset($myscheduling[$value['start_time']]) && $myscheduling[$value['start_time']]['status'] != 2 && $myscheduling[$value['start_time']]['status'] != $value['status']){
                $update_data[] = ['id'=>$value['id'],'status'=>$myscheduling[$value['start_time']]['status']];
            }
        }
        if($date8){
            foreach ($date8 as $key => $value) {
                $start = date('Y-m-d',$value);
                foreach ($myscheduling as $k => $val) {
                    $update_data[] = [
                        'start_time' => strtotime($start . ' ' . $val['start_time']),
                        'technician_id' => $t_id,
                        'status' => $val['status'] == 2 ? 0 : $val['status'],
                        'scheduling_date' => $value,
                    ];
                }    
            }
        }
        if($update_data){
            $res = (new myModel())->saveAll($update_data);
        }
        return show(200,'设置成功');
    }
    private function getBusiness($business_start,$business_end,$date,$t_id,$arr){
        $item = $business_start;
        $start = strtotime($date . ' ' . $item);
        $arr[] = ['start_time'=>$start,'status'=>0,'technician_id'=>$t_id,'scheduling_date'=>strtotime($date)];
        if($start+1800 < strtotime($date . ' ' . $business_end)){
            return $this->getBusiness(date('H:i',$start+1800),$business_end,$date,$t_id,$arr);
        }
        return $arr;
    }
}